const query = require('../db');
const util = require('../util');
// import {User} from '../User';
let user = {
    user_id:null,
user_number:null,
phone:null,
name:null,
fk_classId:null,
sex:null,
user_type:null,
status:1,
problme_id:null,
password:null,
answer:null,
newPassword:null
};
module.exports = {
    //学生登录
    login (data,callback) {
        let student_no = data.user_number;
        let student_psd = data.password;
        let sql =  `select * FROM \`user\` WHERE user_number = '${student_no}' AND password = '${student_psd}'`;
        query(sql,(err,data)=>{
            callback(err,data);
        })
    },
    //根据学生id获取学生信息
    info (studentGuid,callback) {
        let sql = `select * FROM \`user\` WHERE user_number = '${studentGuid}'`;
        query(sql,(err,data)=>{
            callback(err,data);
        })
    },
    //用户注册
    register(data,callback){
        user=data;
        let isAdmin = 0;
        if(user.user_type=='教师'){
            isAdmin=1
        }
        let sql = `INSERT INTO \`user\`(user_number,phone,\`name\`,fk_classId,sex,user_type,isAdmin,\`password\`,problme_id,answer) 
        VALUES('${user.user_number}','${user.phone}','${user.name}',${user.fk_classId},'${user.sex}','${user.user_type}',${isAdmin},'${user.password}',${user.problme_id},'${user.answer}')`;
          query(sql,(err,data)=>{
              if(err){
                  console.log(err);
              }else {
                  callback(data);
              }
          })
    },
    //密码修改
    updatePassWord(data,callback){
        user=data;
        let sql = `UPDATE \`user\` SET \`password\`='${user.newPassword}' WHERE user_number='${user.user_number}'`;
        query(sql,(err,data)=>{
             if(err){
                 console.log(err);
             }else {
                 callback(data);
             }
        })
    },
    //个人基本信息修改,修改密保答案，问题，联系方式等
    update(data,callback){
      user =data;
      let  sql = `UPDATE \`user\` SET phone='${user.phone}',problme_id=${user.problme_id},answer='${user.answer}' WHERE user_number='${user.user_number}'`;
      query(sql,(err,data)=>{
          if(err){
              console.log(err);
          }else {
              callback(data);
          }
      })
    },
    //找回密码
    findPassWord(data,callback){
        user= data;
        let sql = `UPDATE \`user\` SET \`password\`='${user.password}' WHERE user_number='${user.user_number}' AND problme_id=${user.problme_id} AND answer='${user.answer}'`;
        query(sql,(err,data)=>{
            if(err){
                console.log(err);
            }else {
                callback(data);
            }
        })
    },
    //获取密保问题
    getProblemList(callback){
        let sql = 'SELECT * FROM problem';
        query(sql,(err,data)=>{
            if(err){
                console.log(err);
            }else {
                callback(data);
            }
        })
    }
};
